home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / libtabl.c < prev    next >
C/C++ Source or Header  |  1998-06-14  |  10KB  |  398 lines

  1. /*
  2. *                               libtabl.c
  3. *
  4. * Code to handle CTDLTABL.SYS
  5. */
  6. /*
  7. *                               history
  8. *
  9. * 87Jan20 HAW  Integrity stuff for portability.
  10. * 86Apr24 HAW  Modified for fwrite() and fread().
  11. * 85Nov15 HAW  Created.
  12. */
  13. #include "ctdl.h"
  14. /*
  15. *                               Contents
  16. *
  17. *       readSysTab()            restores system state from ctdltabl.sys
  18. *       common_read()           bottleneck for reading
  19. *       writeSysTab()           saves state of system in CTDLTABL.SYS
  20. *       GetDynamic()            allocation bottleneck
  21. */
  22. CONFIG          cfg;                    /* A buncha variables   */
  23. LogTable        *logTab;                /* RAM index of pippuls */
  24. NetTable        *netTab;                /* RAM index of nodes   */
  25. rTable          *roomTab;               /* RAM index of rooms   */
  26. EVENT           *EventTab = NULL;
  27. char            *indexTable = "ctdlTabl.sys";
  28. struct floor    *FloorTab;
  29. int             TopFloor;
  30.  
  31. static struct
  32.   {
  33.   int checkMark;                        /* rudimentary integrity */
  34.   int cfgSize;                  /* sizeof cfg   */
  35.   int logTSize;                 /* logtab size  */
  36.   int endMark;                  /* another integrity check      */
  37.  
  38.   }
  39. integrity;
  40. extern char *R_W_ANY;
  41. static void *FindServes(char *name, char *target);
  42. SListBase Serves =
  43.   {
  44.   NULL, FindServes, NULL, free, NULL
  45.  
  46.   };
  47. /**
  48.   These are the "markers" for the version id of the
  49.   current configuration.  If you change the internals
  50.   of the ctdltbl.sys file, you should increment both
  51.   of these to ensure that everyone is using the
  52.   correct versions of CTDL and CONFG.
  53. **/
  54. #define CHKM    8       /* major release        */
  55. #define ENDM    9
  56. /*
  57. * readSysTab()
  58. *
  59. * This function restores the state of system from CTDLTABL.SYS
  60. * returns:              TRUE on success, else FALSE
  61. * destroys CTDLTABL.TAB after read, to prevent erroneous re-use
  62. * in the event of a crash.
  63. *
  64. * MS-DOS fun: Here's the map --
  65. * Word 1 == sizeof cfg
  66. * Word 2 == sizeof logTab
  67. * Word 3 == sizeof roomTab
  68. * Word 4 -- thru x == cfg contents
  69. * x -- y == logTab
  70. * y -- z == roomTab
  71. * z -- a == netTab
  72. * EOF
  73. */
  74. char readSysTab(char kill, char showMsg)
  75.   {
  76.   FILE  *fd;
  77.   extern char *READ_ANY;
  78.   int           rover;
  79.   long  bytes;
  80.   SYS_FILE    name;
  81.   char  caller;
  82.   label       temp;
  83.   caller = cfg.weAre;
  84.   if ((fd = fopen(indexTable, READ_ANY)) == NULL)
  85.     {
  86.     if (showMsg)
  87.       {
  88.       printf("%s not found or read protected!", indexTable);    /* Tsk, tsk! */
  89.       perror("fopen");
  90.       poserr("fopen");
  91.       };
  92.     return(FALSE);
  93.  
  94.     }
  95.   if (fread(&integrity, sizeof integrity, 1, fd) != 1)
  96.     {
  97.     if (showMsg)
  98.       {
  99.       printf("Improper size of %s, unable to process.\n",indexTable);
  100.       perror("fread");
  101.       poserr("fread");
  102.       };
  103.     return FALSE;
  104.  
  105.     }
  106.   if (     integrity.checkMark != CHKM ||
  107.   integrity.endMark != ENDM ||
  108.   integrity.cfgSize != sizeof cfg)
  109.     {
  110.     if (showMsg) printf("Improper integrity information, you need the CONFG and CTDL\n");
  111.     return(FALSE);
  112.  
  113.     }
  114.   if (!common_read(&cfg, (sizeof cfg), 1, fd, showMsg))  return FALSE;
  115.   /* Allocations for dynamic parameters */
  116.   logTab = (LogTable *) GetDynamic(integrity.logTSize);
  117.   roomTab = (rTable *) GetDynamic(MAXROOMS * (sizeof (*roomTab)));
  118.   if (cfg.netSize)
  119.   netTab = (NetTable *) GetDynamic(sizeof (*netTab) * cfg.netSize);
  120.   else
  121.   netTab = NULL;
  122.   if (cfg.EvNumber) EventTab  = (EVENT *)GetDynamic(sizeof (*EventTab) * cfg.EvNumber);
  123.   /* "- 1" is kludge */
  124.   if (integrity.logTSize != sizeof (*logTab) * cfg.MAXLOGTAB)
  125.     {
  126.     if (showMsg) printf("Improper size of Log Table in integrity check\n");
  127.     return(FALSE);
  128.  
  129.     }
  130.   if (!common_read(logTab, integrity.logTSize, 1, fd, showMsg))
  131.   return FALSE;
  132.   if (!common_read(roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd, showMsg))
  133.   return FALSE;
  134.   if (cfg.netSize)
  135.     {
  136.     for (rover = 0; rover < cfg.netSize; rover++)
  137.       {
  138.       if (!common_read(&netTab[rover], NT_SIZE, 1, fd, showMsg)) return FALSE;
  139.       netTab[rover].netTRooms = (SharedRoom *) GetDynamic(SR_BULK);
  140.       if (!common_read(netTab[rover].netTRooms, SR_BULK, 1, fd, showMsg)) return FALSE;
  141.       };
  142.  
  143.     }
  144.   if (cfg.EvNumber)
  145.     {
  146.     if (!common_read(EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd, showMsg))
  147.     return FALSE;
  148.  
  149.     };
  150.   for (rover = 0; rover < cfg.DomainHandlers; rover++)
  151.     {
  152.     if (!common_read(temp, NAMESIZE, 1, fd, showMsg))
  153.       {
  154.       return FALSE;
  155.  
  156.       }
  157.     AddData(&Serves, strdup(temp), NULL, FALSE);
  158.  
  159.     }
  160.   fclose(fd);
  161.   makeSysName(name, "ctdlflr.sys", &cfg.floorArea);
  162.   if ((fd = fopen(name, R_W_ANY)) == NULL)
  163.     {
  164.     if (caller != CONFIGUR)
  165.       {
  166.       if (showMsg)
  167.         {
  168.         printf("No floor table file found!\n");
  169.         perror("fopen");
  170.         poserr("fopen");
  171.         };
  172.       return FALSE;
  173.  
  174.       }
  175.  
  176.     }
  177.   else
  178.     {
  179.     totalBytes(&bytes, fd);
  180.     FloorTab = (struct floor *) GetDynamic((int) bytes);
  181.     if (fread(FloorTab, (int) bytes, 1, fd) != 1)
  182.       {
  183.       if (showMsg)
  184.         {
  185.         printf("error reading floor tab file\n");
  186.         perror("fread");
  187.         poserr("fread");
  188.         };
  189.  
  190.       fclose(fd);
  191.       if (caller != CONFIGUR) return FALSE;
  192.  
  193.       }
  194.     else
  195.       {
  196.       fclose(fd);
  197.       TopFloor = (int) bytes/sizeof(*FloorTab);
  198.  
  199.       }
  200.  
  201.     }
  202.   if (kill) unlink(indexTable);
  203.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  204.   return(TRUE);
  205.  
  206.   }
  207. /*
  208. * common_read()
  209. *
  210. * This function reads in from file the important stuff.
  211. * returns:      TRUE on success, else FALSE
  212. */
  213. int common_read(void *block, int size, int elements, FILE *fd,
  214. char showMsg)
  215.   {
  216.   if (size == 0) return TRUE;
  217.   if (fread(block, size, elements, fd) != 1)
  218.     {
  219.     if (showMsg)
  220.       {
  221.       printf("Unable to read common data %d elements of size %d\n",elements, size);
  222.       perror("fread");
  223.       poserr("fread");
  224.       };
  225.     return FALSE;
  226.  
  227.     }
  228.   return TRUE;
  229.  
  230.   }
  231. static FILE *fd;
  232. /*
  233. * writeSysTab()
  234. *
  235. * This saves state of system in CTDLTABL.SYS
  236. * returns:      TRUE on success, else ERROR
  237. * See readSysTab() to see what the CTDLTABL.SYS map looks like.
  238. */
  239. static void WriteServers(char *name);
  240. int writeSysTab()
  241.   {
  242.   extern char   *WRITE_ANY;
  243.   int   rover;
  244.   if ((fd = fopen(indexTable, WRITE_ANY)) == NULL)
  245.     {
  246.     printf("writeSysTab: cannot open %s for writing\n",indexTable);
  247.     perror("writeSysTab");
  248.     poserr("writeSysTab");
  249.     return(ERROR);
  250.  
  251.     }
  252.   /* Write out some key stuff so we can detect bizarreness: */
  253.   integrity.checkMark = CHKM;
  254.   integrity.endMark = ENDM;
  255.   integrity.cfgSize = sizeof cfg;
  256.   integrity.logTSize = sizeof (*logTab) * cfg.MAXLOGTAB;
  257.  
  258.   if( 1 != fwrite((char *)&integrity, (sizeof integrity), 1, fd) )
  259.     {
  260.     printf("writeSysTab: problem writing integrity data\n");
  261.     perror("writeSysTab");
  262.     poserr("writeSysTab");
  263.     return(ERROR);
  264.     };
  265.  
  266.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  267.   if( 1 != fwrite((char *)&cfg, (sizeof cfg), 1, fd) )
  268.     {
  269.     printf("writeSysTab: problem writing configuration data\n");
  270.     perror("writeSysTab");
  271.     poserr("writeSysTab");
  272.     return(ERROR);
  273.     };
  274.  
  275.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  276.   if( 1 != fwrite((char *)logTab, (sizeof(*logTab) * cfg.MAXLOGTAB), 1, fd))
  277.     {
  278.     printf("writeSysTab: problem writing User Log table\n");
  279.     perror("writeSysTab");
  280.     poserr("writeSysTab");
  281.     return(ERROR);
  282.     };
  283.  
  284.   if( 1 != fwrite((char *)roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd))
  285.     {
  286.     printf("writeSysTab: problem writing room table\n");
  287.     perror("writeSysTab");
  288.     poserr("writeSysTab");
  289.     return(ERROR);
  290.     };
  291.  
  292.    for (rover = 0; rover < cfg.netSize; rover++)
  293.     {
  294.     if( 1 != fwrite((char *)&netTab[rover], NT_SIZE, 1, fd))
  295.       {
  296.       printf("writeSysTab: problem writing Net Table[%d]\n",rover);
  297.       perror("writeSysTab");
  298.       poserr("writeSysTab");
  299.       return(ERROR);
  300.       };
  301.  
  302.     if( 1 != fwrite((char *)netTab[rover].netTRooms, SR_BULK, 1, fd))
  303.       {
  304.       printf("writeSysTab: problem writing Net Table/Room[%d]\n",rover);
  305.       perror("writeSysTab");
  306.       poserr("writeSysTab");
  307.       return(ERROR);
  308.       };
  309.  
  310.     };
  311.   if (cfg.EvNumber)
  312.   if( 1 != fwrite((char *)EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd))
  313.     {
  314.     printf("writeSysTab: problem writing Event Table\n");
  315.     perror("writeSysTab");
  316.     poserr("writeSysTab");
  317.     return(ERROR);
  318.     };
  319.  
  320.   RunList(&Serves, WriteServers);
  321.   fclose(fd);
  322.   return(TRUE);
  323.  
  324.   }
  325. /*
  326. * WriteServers()
  327. *
  328. * This function writes a domain server out to ctdltabl.sys.  See DOMAINS.C
  329. * for more information on this list.
  330. */
  331. static void WriteServers(char *name)
  332.   {
  333.   if( 1 != fwrite((char *)name, NAMESIZE, 1, fd))
  334.     {
  335.     printf("WriteServers: problem writing server data\n");
  336.     perror("writeServers");
  337.     poserr("writeServers");
  338.     };
  339.   }
  340. /*
  341. * GetDynamic()
  342. *
  343. * This does mallocs with error checking.
  344. */
  345. void *special_GetDynamic(unsigned size, char *file, int line)
  346.   {
  347.   void *temp;
  348.   char msg[80];
  349. /**
  350.   if (cfg.BoolFlags.debug)
  351.     {
  352.     splitF(NULL,"GetDynamic(%04.4x, %s, %d)\n",size,file,line);
  353.     };
  354. **/
  355.   if (size == 0) return NULL; /* Simplify code  */
  356.   temp = calloc(1,size);
  357.   /* printf("Requested %d bytes, received address %p\n", size, temp); */
  358.   if (temp == NULL)
  359.     {
  360.     printf("Request for %u bytes of memory failed.\n", size);
  361.     sprintf(msg, "Asked for %u bytes, unable to get it.\n", size);
  362.     crashout(msg);
  363.     }
  364.   return temp;
  365.  
  366.   }
  367. /*
  368. * openFile()
  369. *
  370. * This opens one of the .sys files.
  371. */
  372. void openFile(char *filename, FILE **fd)
  373.   {
  374.   /* We use fopen here rather than safeopen for link reasons */
  375.   if ((*fd = fopen(filename, R_W_ANY)) == NULL)
  376.     {
  377.     printf("?no %s, cannot open it", filename);
  378.     exit(SYSOP_EXIT);
  379.  
  380.     }
  381.  
  382.   }
  383. /*
  384. * FindServes()
  385. *
  386. * This is a find the server function.  It's used in the list of domain servers
  387. * to allow us to search for a domain server based on the name of the domain.
  388. */
  389. static void *FindServes(char *name, char *target)
  390.   {
  391.   if (cfg.BoolFlags.debug)
  392.     {
  393.     splitF(NULL,"FindServes( %s, %s)\n",name, target);
  394.     };
  395.   return (strCmpU(name, target) == SAMESTRING) ? name : NULL;
  396.  
  397.   }
  398.